package br.com.fogas.medicao.activity.helper;

import br.com.fogas.database.FogasDatabase;
import br.com.fogas.medicao.R;
import br.com.fogas.utils.dialog.DialogAlert;
import br.com.fogas.utils.dialog.ProgressDialogBuilder;
import br.com.fogas.utils.task.ServiceTask;
import br.com.fogas.medicao.activity.dialog.DialogConfigSenhaAdm;
import br.com.fogas.medicao.activity.dialog.DialogConfigSenhaAdm.DialogConfigSenhaAdmListener;
import br.com.fogas.medicao.application.ApplicationFogas;
import br.com.fogas.medicao.business.ConfigurationBusiness;
import br.com.fogas.modelo.AppDados;
import br.com.fogas.utils.StringUtility;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

public class ConfiguracaoHelper {

	private FragmentActivity activity;
	
	private FogasDatabase database;

	private LinearLayout contentConfig;

	private CheckBox cbxConfigSincNfe;

	private EditText etConfigTentativasLeiQrcode;

	private EditText etConfigTempoEsperaAutNfe;

	private EditText etConfigTempoProcSync;

	private EditText etConfigSenhaAdm;

	private Button btnConfigSalvar;
	
    private Handler handler;

	private Button btnConfigAbrirDlg;
	
	public ConfiguracaoHelper(FragmentActivity activity, View view) {
		this.activity = activity;
		this.database = new FogasDatabase(view.getContext());
		this.handler = new Handler();
		this.contentConfig = (LinearLayout) view.findViewById(R.id.contentConfig);
		this.cbxConfigSincNfe = (CheckBox) view.findViewById(R.id.cbxConfigSincNfe);
		this.etConfigTentativasLeiQrcode = (EditText) view.findViewById(R.id.etConfigTentativasLeiQrcode);
		this.etConfigTempoEsperaAutNfe = (EditText) view.findViewById(R.id.etConfigTempoEsperaAutNfe);
		this.etConfigTempoProcSync = (EditText) view.findViewById(R.id.etConfigTempoProcSync);
		this.etConfigSenhaAdm = (EditText) view.findViewById(R.id.etConfigSenhaAdm);
		this.btnConfigSalvar = (Button) view.findViewById(R.id.btnConfigSalvar);
		this.btnConfigSalvar.setOnClickListener(salvarClick);
		this.btnConfigAbrirDlg = (Button) view.findViewById(R.id.btnConfigAbrirDlg);
		this.btnConfigAbrirDlg.setOnClickListener(abrirDialogSenhaAdm);
		this.atualizarCampos();
		this.ocultarViews();
		this.abrirDialogSenhaAdm();
	}
	
	View.OnClickListener salvarClick = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			salvarConfiguracao();
		}
	};
	
	View.OnClickListener abrirDialogSenhaAdm = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			abrirDialogSenhaAdm();
		}
	};
	
	private void salvarConfiguracao() {
		ConfigurationBusiness business = new ConfigurationBusiness(this);
		
		ProgressDialogBuilder dialog = new ProgressDialogBuilder(getActivity())
		.setMessage(getActivity().getString(br.com.fogas.utils.R.string.dialog_processando));
		
		ServiceTask task = new ServiceTask(dialog, business);
		task.execute();
	}
	
	private void abrirDialogSenhaAdm() {
		DialogConfigSenhaAdm.newInstance(listenerSenhaAdm)
		.showDialog(activity.getSupportFragmentManager());
	}

	DialogConfigSenhaAdm.DialogConfigSenhaAdmListener listenerSenhaAdm = new DialogConfigSenhaAdmListener() {
		
		@Override
		public void onPositiveClick(DialogFragment dialog, String senha) {
			String senhaAdm = ApplicationFogas.getInstance().getConfiguracao().getAppDados().getSenha_adm();
			boolean senhaCorreta = StringUtility.equals(senhaAdm, senha);
			if(senhaCorreta) {
				dialog.dismiss();
				mostrarViews();
			} else {
				DialogInterface.OnClickListener listener = new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						abrirDialogSenhaAdm();
					}
				};
				
				dialog.dismiss();
				DialogAlert.newInstance()
				.setTitle(R.string.dialog_title_alert)
				.setMessage(R.string.dialog_config_message_error_senha)
				.setPositiveClick(listener)
				.showDialog(activity.getSupportFragmentManager());
			}
		}

		@Override
		public void onNegativeClick(DialogFragment dialog, String senha) {
			dialog.dismiss();
		}
	};
	
	public void atualizarCampos() {
		this.handler.post(new Runnable() {
			public void run() {
				AppDados appDados = ApplicationFogas.getInstance().getConfiguracao().getAppDados();
				cbxConfigSincNfe.setChecked(appDados.isAutorizar_nfe_online());
				etConfigTentativasLeiQrcode.setText(String.valueOf(appDados.getTentativas_leitura_qrcode()));
				etConfigTempoEsperaAutNfe.setText(String.valueOf(appDados.getTempo_espera_aut_nfe()));
				etConfigTempoProcSync.setText(String.valueOf(appDados.getTempo_processamento_sync()));
			}
		});
	}
	
	private void ocultarViews() {
		this.contentConfig.setVisibility(View.GONE);
		this.btnConfigSalvar.setVisibility(View.GONE);
		this.btnConfigAbrirDlg.setVisibility(View.GONE);
		this.btnConfigAbrirDlg.setVisibility(View.VISIBLE);
	}
	
	private void mostrarViews() {
		this.contentConfig.setVisibility(View.VISIBLE);
		this.btnConfigSalvar.setVisibility(View.VISIBLE);
		this.btnConfigAbrirDlg.setVisibility(View.GONE);
	}

	public FragmentActivity getActivity() {
		return activity;
	}
	
	public FogasDatabase getDatabase() {
		return database;
	}

	public LinearLayout getContentConfig() {
		return contentConfig;
	}

	public CheckBox getCbxConfigSincNfe() {
		return cbxConfigSincNfe;
	}

	public EditText getEtConfigTentativasLeiQrcode() {
		return etConfigTentativasLeiQrcode;
	}

	public EditText getEtConfigTempoEsperaAutNfe() {
		return etConfigTempoEsperaAutNfe;
	}

	public EditText getEtConfigTempoProcSync() {
		return etConfigTempoProcSync;
	}

	public EditText getEtConfigSenhaAdm() {
		return etConfigSenhaAdm;
	}

	public Button getBtnConfigSalvar() {
		return btnConfigSalvar;
	}
}